//
//  EditPsdViewController.m
//  Kfangchan
//
//  Created by 朋飞 on 14-11-13.
//  Copyright (c) 2014年 Beijing Renjuren Network Technology Co., Ltd. All rights reserved.
//

#import "EditPsdViewController.h"
#pragma mark 修改密码
#pragma mark

@interface EditPsdViewController ()
@property (strong, nonatomic) IBOutlet UIButton *saveBtn;
@property (strong, nonatomic) IBOutlet UIButton *getmsgBtn;


@property (strong, nonatomic) IBOutlet UITextField * msgTextField;
@property (strong, nonatomic) IBOutlet UITextField * psdTextField;
@property (strong, nonatomic) IBOutlet UITextField * confirmPsdTextField;


@end

@implementation EditPsdViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 返回
    self.navigationItem.titleView = [[Theam currentTheam] navigationTitleViewWithTitle:@"修改密码"];
    self.navigationItem.leftBarButtonItem = [[Theam currentTheam] navigationBarLeftButtonItemWithImage:[UIImage imageNamed:@"nav_back.png"] Target:self Selector:@selector(back)];
    
    self.navigationItem.rightBarButtonItem = [[Theam currentTheam] navigationBarRightButtonItemWithImage:nil Title:@"确认修改" Target:self Selector:@selector(save)];
     [self.getmsgBtn.layer setCornerRadius:5.0];
    
    
}

#pragma mark  确认修改
#pragma mark
-(void)save {
    BOOL  verify = [self verify];
    if(verify){
        //弱引用
        __weak EditPsdViewController *weak_self = self;
        // 构建请求参数
        NSDictionary *param = @{
                                @"passwd":self.psdTextField.text,
                                @"vericode":self.msgTextField.text
                                };
        // 请求
        MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_UPDATE completionHandler:^(id responseObject) {
            
            if(responseObject){
                NSString *str  =   [responseObject objectForKey:@"KFC_AGENT_PWD"];
               // NSDictionary* dic = [AppConfig getAgent];
                NSMutableDictionary* dic = [[AppConfig getAgent] mutableCopy];
                [dic setValue:str forKey:@"KFC_AGENT_PWD"];
                [AppConfig setAgent:dic];
                
                [weak_self back];
                
            }
            
        } errorHandler:^(NSError *error){
            
            showCustomAlertMessage(@"网路错误");
            
        }finishHandler:^(id responseObject) {
            
        }];
        
        [self.opHandlers addObject:operation];
        
    }
    
}


#pragma mark -
#pragma mark 关闭键盘
- (IBAction)hideKeyboard {
    [self.msgTextField resignFirstResponder];
    [self.psdTextField resignFirstResponder];
    [self.confirmPsdTextField resignFirstResponder];
    
}

-(BOOL) verify{
    NSString *msgcode = self.msgTextField.text;
    NSString *psd = self.psdTextField.text;
    NSString *confirmpsd = self.confirmPsdTextField.text;
    
    
    UIAlertView *alert = nil;
    
    
    if(msgcode.length!=4){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入正确的验证码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    if(psd.length==0){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请输入新密码" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    if(![psd isEqualToString:confirmpsd]){
        alert = [[UIAlertView alloc]initWithTitle:nil message:@"请确保新密码跟确认密码一致" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
        [alert show];
        return NO;
    }
    
    
    //    if(password.length<6){
    //        alert = [[UIAlertView alloc]initWithTitle:nil message:@"用户名不少于6个字符" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil];
    //        [alert show];
    //        return NO;
    //    }
    //
    
    
    
    
    return YES ;
}


#pragma mark  获得短信验证码
#pragma mark
-(IBAction) getMsgcode:(id)sender{
    // 构建请求参数
    NSDictionary *param = @{
                            @"mobile":[AppConfig getAgent][@"mobile"]
                            };
    // 请求
    MKNetworkOperation *operation = [[BaseEngine sharedEngine]RunRequest:[param mutableCopy] path:KFC_VERIFY_PHONE completionHandler:^(id responseObject) {
        if(responseObject){
            [self startTime60];
        }
        
    } errorHandler:^(NSError *error){
        
        showCustomAlertMessage(@"网路错误");
        
    }finishHandler:^(id responseObject) {
        
    }];
    
    [self.opHandlers addObject:operation];
    
}



#pragma mark 返回
#pragma mark
- (void)back {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}
-(void)startTime60{
    
    __block int timeout=60; //倒计时时间
    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_source_t _timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0,queue);
    dispatch_source_set_timer(_timer,dispatch_walltime(NULL, 0),1.0*NSEC_PER_SEC, 0); //每秒执行
    dispatch_source_set_event_handler(_timer, ^{
        if(timeout<=0){ //倒计时结束，关闭
            dispatch_source_cancel(_timer);
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.getmsgBtn.layer setCornerRadius:5.0];
                self.getmsgBtn.backgroundColor =[UIColor colorWithHexString:@"66b97a"];
                [self.getmsgBtn setTitle:@"重新发送" forState:UIControlStateNormal];
                self.getmsgBtn.userInteractionEnabled = YES;
            });
        }else{
            //            int minutes = timeout / 60;
            
            int seconds = timeout % 61;
            NSString *strTime = [NSString stringWithFormat:@"%.2d", seconds];
            dispatch_async(dispatch_get_main_queue(), ^{
                //设置界面的按钮显示 根据自己需求设置
                [self.getmsgBtn.layer setCornerRadius:5.0];
                self.getmsgBtn.backgroundColor =[UIColor colorWithHexString:@"949494"];
                [self.getmsgBtn setTitle:[NSString stringWithFormat:@"%@秒后重新发送",strTime] forState:UIControlStateNormal];
                self.getmsgBtn.userInteractionEnabled = NO;
                
                
            });
            timeout--;
            
        }
    });
    dispatch_resume(_timer);
    
}


@end
